package com.app.common.utils;




import java.io.InputStream;

import javax.xml.transform.TransformerException;

import com.sun.org.apache.xpath.internal.XPathAPI;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.css.sword.platform.comm.log.LogWritter;
import com.css.sword.platform.utils.SwordLogUtils;
/**
 * 文件配置类
 * @author cuichr
 *
 */

public class FileDomainConfig {
    private static Document fileDomainConfigDom = null;
    private static FileDomainConfig cfg = new FileDomainConfig();
    private static LogWritter log = SwordLogUtils.getLogger(FileDomainConfig.class);

    private FileDomainConfig() {
        try {
            InputStream in = getClass().getResourceAsStream(
                             "/FileDomainConfig.xml") ;
            fileDomainConfigDom = XMLAdapter.load(in) ;
            in.close() ;
        }
        catch (Exception exception) {
            //System.out.println("Can't read the properties file. ") ;
            log.error("Can't read the properties file. " +
                exception.getMessage()) ;
        }
    }

    public static FileDomainConfig getInstance()
    {
        return cfg;
    }
    /**
     * Use singleton pattern, only return one instance of Configuration.
     * @return Configuration
     */
    public  Document getConfigDom() {
      return fileDomainConfigDom;
    }
    @SuppressWarnings("static-access")
	public void setConfigDom(Document dom){
        this.fileDomainConfigDom = dom;
    }
    public Element getOrgElement(String orgAlias){
        try {
            Element orgE = (Element)XPathAPI.selectSingleNode(fileDomainConfigDom.getDocumentElement()
                           , "Org[@Alias='" + orgAlias + "']") ;
            if(null==orgE && "share".equals(orgAlias))
                orgE=fileDomainConfigDom.getDocumentElement();

            return orgE;
        }
        catch (TransformerException ex) {
            return null;
        }
    }
    public String getConfigURL(String orgAlias , String configName , String eleName,String itemName){
        Element configE=this.getConfigElement(orgAlias,configName,eleName);
        if(null!=configE){
        	Element itemE = null;
			try {
				itemE = (Element)XPathAPI.selectSingleNode(configE, "URL[@Name='" + itemName + "']");
			} catch (TransformerException e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
        	if(null != itemE)
        		return itemE.getAttribute("Value");
        }
        return null;
    }
    //返回一个配置项目
    public Element getConfigElement(String orgAlias , String configName , String eleName) {
        Element configElement = null ;
        if (null != orgAlias && !"".equals(orgAlias)) {
            try {
                Element orgE=this.getOrgElement(orgAlias);
                if (null != orgE) {
                    configElement = (Element)XPathAPI.selectSingleNode(orgE
                                    , configName + "List/" + configName + "[@Name='" + eleName
                                    + "']") ;
                }
            }
            catch (TransformerException ex) {
            }
        }
        try {
            if (null == configElement) {
                configElement = (Element)XPathAPI.selectSingleNode(fileDomainConfigDom.getDocumentElement()
                                , configName + "List/" + configName + "[@Name='" + eleName
                                + "']") ;
            }
        }
        catch (TransformerException ex1) {
        }
        return configElement ;
    }
    //返回一个配置内容
    public String getConfigValue(String orgAlias , String configName , String eleName,String attName) {
        Element configE=this.getConfigElement(orgAlias,configName,eleName);
        if(null!=configE)
            return configE.getAttribute(attName);
        else
            return null;
    }

}
